//
//  HomePageMenuTableViewHeaderFooterView.m
//  GLGW_Shoping
//
//  Created by yang on 2018/11/29.
//  Copyright © 2018年 yang. All rights reserved.
//

#import "HomePageMenuTableViewHeaderFooterView.h"
#import "SPPageMenu.h"
#import "HomecategoryproductinfosModel.h"

#define pageMenuH kHeightProportion_In_iPhoneTriploid(44)

@interface HomePageMenuTableViewHeaderFooterView ()<SPPageMenuDelegate, UIScrollViewDelegate>{
    NSInteger selectIndex;
}
@property (nonatomic, strong) NSMutableArray *dataArr;
@property (nonatomic, weak) SPPageMenu *pageMenu;

@property (nonatomic, strong) UIScrollView *categoryScroll;

@end

@implementation HomePageMenuTableViewHeaderFooterView

-(instancetype)initWithReuseIdentifier:(NSString *)reuseIdentifier{
    if (self = [super initWithReuseIdentifier:reuseIdentifier]) {
        self.dataArr = [NSMutableArray arrayWithCapacity:0];
        GLGWView * bankgroundView = [[GLGWView alloc] initWithInSuperView:self.contentView];
        bankgroundView.backgroundColor = [UIColor whiteColor];
        [bankgroundView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.bottom.right.equalTo(self.contentView).offset(0);
            make.top.equalTo(self.contentView.mas_top).offset(kHeightProportion_In_iPhoneTriploid(10));
        }];
        //
        GLGWLabel * tuiJianLabel = [[GLGWLabel alloc] initWithFont:BOLDSYSTEMFONT(18) theColor:kColor_Rule_Black inSuperView:bankgroundView];
        tuiJianLabel.text = @"优质商品推荐";
        [tuiJianLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(bankgroundView.mas_left).offset(kWidthProportion_In_iPhoneTriploid(15));
            make.top.equalTo(bankgroundView.mas_top).offset(kHeightProportion_In_iPhoneTriploid(20));
            make.height.equalTo(@(kHeightProportion_In_iPhoneTriploid(25)));
        }];
        //
        GLGWButton * moneBut = [[GLGWButton alloc] initWithTitle:@"查看更多 " titleLableFont:SYSTEMFONT(14) titleLableColor:[UIColor colorWithHexString:@"#999999"] inSuperView:bankgroundView];
        moneBut.image_Name = @"icon_gengduo";
        [moneBut setImagePositionWithType:SSImagePositionTypeRight spacing:0];
        [moneBut mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(bankgroundView.mas_right).offset(-kWidthProportion_In_iPhoneTriploid(15));
            make.top.equalTo(bankgroundView.mas_top).offset(kHeightProportion_In_iPhoneTriploid(23));
            make.height.equalTo(@(kHeightProportion_In_iPhoneTriploid(20)));
        }];
        //
        // trackerStyle:跟踪器的样式
        SPPageMenu *pageMenu = [SPPageMenu pageMenuWithFrame:CGRectMake(0, kHeightProportion_In_iPhoneTriploid(46), KScreenWidth, pageMenuH) trackerStyle:SPPageMenuTrackerStyleLineAttachment];
        pageMenu.backgroundColor = [UIColor whiteColor];
        pageMenu.selectedItemTitleColor = [UIColor colorWithHexString:@"#A89A60"];
        pageMenu.unSelectedItemTitleColor = [UIColor colorWithHexString:@"#999999"];
        pageMenu.itemTitleFont = SYSTEMFONT(14);
        pageMenu.itemPadding = kWidthProportion_In_iPhoneTriploid(18);
        pageMenu.trackerWidth = kWidthProportion_In_iPhoneTriploid(27);
        pageMenu.tracker.backgroundColor = [UIColor colorWithHexString:@"#A89A60"];
        pageMenu.permutationWay = SPPageMenuPermutationWayScrollAdaptContent;
        pageMenu.dividingLine.backgroundColor = [UIColor colorWithHexString:@"#EAEAEA"];
        pageMenu.dividingLineHeight = 1.0;
        // 传递数组，默认选中第2个

        // 设置代理
        pageMenu.delegate = self;
        // 给pageMenu传递外界的大scrollView，内部监听self.scrollView的滚动，从而实现让跟踪器跟随self.scrollView移动的效果
        [bankgroundView addSubview:pageMenu];
        _pageMenu = pageMenu;
    }
    return self;
}

-(void)pageEmuDataArr:(NSMutableArray *)arr theSelectIndex:(NSInteger)index{
    [self.dataArr removeAllObjects];
    selectIndex = index;
    for (HomecategoryproductinfosModel_Category * cartModel in arr) {
        [self.dataArr addObject:cartModel.categoryName];
    }
    [_pageMenu setItems:self.dataArr selectedItemIndex:selectIndex];
}

#pragma mark - SPPageMenu的代理方法
- (void)pageMenu:(SPPageMenu *)pageMenu itemSelectedFromIndex:(NSInteger)fromIndex toIndex:(NSInteger)toIndex{
    NSLog(@"%zd",toIndex);
    if (fromIndex == toIndex) {
        return;
    }
    if (self.Block_ChickNum) {
        self.Block_ChickNum(toIndex);
    }
}
#pragma mark - scrollViewDelegate

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    
    // 这一步是实现跟踪器时刻跟随scrollView滑动的效果,如果对self.pageMenu.scrollView赋了值，这一步可省
    // [self.pageMenu moveTrackerFollowScrollView:scrollView];
}

@end
